<template>
	<view class="new-container">
		<record-form v-model:userItem="userItem"></record-form>
		<view class="commitBtn">
			<button @click="commitWay()">提交</button>
		</view>
	</view>
</template>

<script setup>
	import recordForm from '../../components/record-form/record-form.vue';
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		addMemberApi,
		searchMemberApi
	} from '../../../api/home-member';
	import {
		getUserInfoApi
	} from '../../../api/login';
import { useUserStore } from '../../../store/user';
	
	const userStore = useUserStore();
	// 登录用户id
	const uid = ref('')

	const recordRef = ref(null);

	const userItem = ref({
		otherId: {
			// birthday:''
		}
	})

	onLoad(() => {
		getUserInfo()

	})

	//新增 提交后跳转
	function commitWay() {
		console.log(userItem.value);
		
		accNewRecord()
		// uni.navigateTo({
		// 	url: '/homepackge/pages/nearbyLnstitutions/nearbyLnstitutions'
		// })
		
		if(userItem.value.relationship == "本人"){
			userStore.getUserInfoAction();
		}
	}


	// 新增档案
	async function accNewRecord() {
		console.log(userItem.value);
		const res = await addMemberApi({
			...userItem.value.otherId,
			relationship: userItem.value.relationship,
			residentId: uid.value
		})
		if (res.code == 200) {

			uni.showToast({
				title: '建档成功',
				icon: 'success',
				duration: 1000
			});

			userItem.value = {
				otherId: {

				}
			};
			
			setTimeout(() => {
				uni.navigateTo({
					url: '/homepackge/pages/nearbyLnstitutions/nearbyLnstitutions'
				})
			}, 1000);

		}
	}




	// 获取用户信息
	const getUserInfo = async () => {

		const res = await getUserInfoApi()
		if (res.code === 200) {
			// console.log(res);

			uid.value = res.data._id
			// console.log(uid.value);
		}
	}
</script>

<style lang="scss" scoped>
	.commitBtn {
		height: 160rpx;
		border-top: 1px solid #eee;
		display: flex;
		align-items: center;
		padding: 0 40rpx;

		button {
			width: 100%;
			height: 88rpx;
			background-color: #2984f8;
			font-size: 16px;
			color: #fff;
			line-height: 88rpx;
			text-align: center;

		}
	}


	.new-container {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		min-height: 100vh;
	}

	// .big-container{

	// }
</style>